package com.power.project.controller;


import cn.dev33.satoken.stp.StpUtil;
import com.power.common.domain.PageDataResponse;
import com.power.common.domain.ResponseDTO;
import com.power.project.api.IWw_userFeign;
import com.power.project.enums.AccountConstant;
import com.power.project.model.Ww_user;
import com.power.project.model.bo.UserRemoveBO;
import com.power.project.model.bo.UserSaveBO;
import com.power.project.model.pageInfo.UserQueryPageInfo;
import com.power.project.model.vo.DepartUserVO;
import com.power.project.model.vo.UserBaseVO;
import com.power.project.model.vo.UserDetailVO;
import com.power.project.service.Ww_userService;
import io.swagger.annotations.Api;
import org.springframework.data.domain.Page;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


/**
 * 控制器
 */

//@Slf4j
//@PowerLog
@RestController
@Api(value = "人员管理", tags = {"人员管理"})
public class Ww_userController implements IWw_userFeign {

    @Resource
    private Ww_userService ww_userService;

    @Override
    public ResponseDTO<PageDataResponse<DepartUserVO>> userList(UserQueryPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo参数不能为空");
        Page<DepartUserVO> page = this.ww_userService.findAll(pageInfo);

        PageDataResponse<DepartUserVO> p = new PageDataResponse<>(page.getContent(), page.getTotalElements(), page.getSize(), Math.max(pageInfo.getPageNumber(), 1));
        return ResponseDTO.succData(p);
    }

    @Override
    public ResponseDTO<PageDataResponse<DepartUserVO>> departUserList(UserQueryPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo参数不能为空");
        Assert.notNull(pageInfo.getDepartmentId(), "departmentId不能为空");
        Page<DepartUserVO> page = this.ww_userService.findAllByDepart(pageInfo);

        PageDataResponse<DepartUserVO> p = new PageDataResponse<>(page.getContent(), page.getTotalElements(), page.getSize(), Math.max(pageInfo.getPageNumber(), 1));
        return ResponseDTO.succData(p);
    }

    @Override
    public ResponseDTO<PageDataResponse<UserBaseVO>> tagUserList(UserQueryPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo参数不能为空");
        Assert.notNull(pageInfo.getTagId(), "tagId不能为空");
        Page<UserBaseVO> page = this.ww_userService.findAllByTag(pageInfo);

        PageDataResponse<UserBaseVO> p = new PageDataResponse<>(page.getContent(), page.getTotalElements(), page.getSize(), Math.max(pageInfo.getPageNumber(), 1));
        return ResponseDTO.succData(p);
    }

    @Override
    public ResponseDTO<UserDetailVO> view(String userId) {
        return ResponseDTO.succData(this.ww_userService.view(userId));
    }

    @Override
    public ResponseDTO<Void> save(UserSaveBO saveBO) {
        this.ww_userService.save(saveBO);
        return ResponseDTO.succ();
    }

    @Override
    public ResponseDTO<Void> delete(String userId) {
        Ww_user wwUser = (Ww_user) StpUtil.getTokenSession().get(AccountConstant.USER);
        if (wwUser.getUserid().equals(userId)) {
            return ResponseDTO.errorNoData("非法操作！不能删除自己");
        }
        ww_userService.deleteByUserId(userId);
        return ResponseDTO.succ();
    }

//    @Override
//    public ResponseDTO<Void> remove(UserRemoveBO removeBO) {
//        this.ww_userService.batchRemove(removeBO);
//        return ResponseDTO.succ();
//    }

    @Override
    public ResponseDTO<Void> batchRemove(UserRemoveBO removeBO) {
        this.ww_userService.batchRemove(removeBO);
        return ResponseDTO.succ();
    }
}
